// pages/cmdView/realTime/realTime.js
const util = require("../../../utils/util");
const appControlAPI = requirePlugin("hello-plugin")
const alertUtil = require("../../../utils/alertUtil")
const {

  // API的状态码
  succeed,
  failed,
  timeout,
  unavailable,
  dataError,
  crcError,
  noRecord,

  // 控制类型
  realStepDataUpload,
  realHeartRateDataUpload,
  realBloodOxygenUpload,
  realBloodPressureUpload,
  realHrvUpload,
  startHrv,

 // 手环返回 APP 启动单次测量结果
 measureStatus,


  receivedRealTimeStep,
  receivedRealTimeHeartRate,
  receivedRealTimePressure,
  receivedRealTimeBloodOxygen,
  receivedRealTimeHrv,


  // 测量控制
  queryHealthDataInvasiveMeasurement,
  stop,
  start,
  // 功能类型
  measureHrData,
  measureBoData,
  measureHrvData,
  measureBpData,
  measurePressData,
  realTimeDataTypeHrv,

} = require("../../../utils/constant")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canvasText: "接收到设备信息："
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  test() {
    this.setData({

    })
  },
  /**
   * 开启实时获取步数
   */
  startQueryStep() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    appControlAPI.realTimeDataUplod(true, realStepDataUpload, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n开启实时获取步数：success"
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n开启实时获取步数：fail"
        })
      }
      alertUtil.closeToast()
    })
    this.queryRealTimeHealthData()
  },
  /**
   * 开启实时获取心率
   */
  startQueryHeartRate() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    appControlAPI.realTimeDataUplod(true, realHeartRateDataUpload, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n开启实时获取心率：success"
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n开启实时获取心率：fail"
        })
      }
      alertUtil.closeToast()
    })
    this.queryRealTimeHealthData()
  },

  /**
   * 开启实时获取hrv
   */
  startQueryHrv() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    appControlAPI.realTimeDataUplod(true, realHrvUpload, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n开启实时获取HRV：success"
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n开启实时获取HRV：fail"
        })
      }
      alertUtil.closeToast()
    })
    this.queryRealTimeHealthData()
  },
  /**
   * 关闭实时获取hrv
   */
  stopQueryHrv() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    appControlAPI.realTimeDataUplod(false, realTimeDataTypeHrv, (state, res) => {
      console.log("停止？", state);
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n关闭实时获取Hrv：success"
        })
        alertUtil.closeToast()
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n关闭实时获取Hrv：fail"
        })
      }
      alertUtil.closeToast()
    })

  },



  /**
   * 关闭实时获取步数
   */
  stopQueryStep() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    appControlAPI.realTimeDataUplod(false, realStepDataUpload, (state, res) => {
      console.log("停止？", state);
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n关闭实时获取步数：success"
        })
        alertUtil.closeToast()
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n关闭实时获取步数：fail"
        })
      }
      alertUtil.closeToast()
    })

  },
  /**
   * 关闭实时获取心率
   */
  stopQueryHeartRate() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    appControlAPI.realTimeDataUplod(false, realHeartRateDataUpload, (state, res) => {
      console.log("停止？", state);
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n关闭实时获取心率：success"
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n关闭实时获取心率：fail"
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 获取实时数据
   */
  queryRealTimeHealthData() {
    alertUtil.handleToast("命令执行中", "loading", 0)
    var that = this
    appControlAPI.receiveRealTimeData(function (realTimeDataType, res) {
      if (realTimeDataType == receivedRealTimeStep) {
        util.printLog(JSON.stringify(res))
        that.setData({
          canvasText: that.data.canvasText + "\n实时步数：" + JSON.stringify(res)
        })
        alertUtil.closeToast()
      } else if (realTimeDataType == receivedRealTimeHeartRate) {
        util.printLog(JSON.stringify(res))
        // that.setData({
          // canvasText: that.data.canvasText + "\n实时心率：" + JSON.stringify(res)
        // })
        alertUtil.closeToast()
      } else if (realTimeDataType == realBloodPressureUpload) {
        util.printLog(JSON.stringify(res))
        that.setData({
          canvasText: that.data.canvasText + "\n实时数据：" + JSON.stringify(res)
        })
        alertUtil.closeToast()
      } else if (realTimeDataType == measureStatus){
        util.printLog(JSON.stringify(res))
        that.setData({
          canvasText: that.data.canvasText + "\n单次测量结果：" + JSON.stringify(res)
        })
        alertUtil.closeToast()
      } else if (realTimeDataType == receivedRealTimePressure){
        util.printLog(JSON.stringify(res))
        that.setData({
          canvasText: that.data.canvasText + "\n实时压力：" + JSON.stringify(res)
        })
        alertUtil.closeToast()
      } else if (realTimeDataType == receivedRealTimeBloodOxygen){
        util.printLog(JSON.stringify(res))
        that.setData({
          canvasText: that.data.canvasText + "\n实时血氧：" + JSON.stringify(res)
        })
        alertUtil.closeToast()
      }
    })
  },

  /**
   * 开始测量心率
   */
  startMeasureHr() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, start, measureHrData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
        this.queryRealTimeHealthData()
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   *手动停止测量心率
   */
  stopMeasureHr() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, stop, measureHrData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },


  /**
   * 开始测量hrv
   */
  startMeasureHrv() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, start, measureHrvData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
        this.queryRealTimeHealthData()
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   *手动停止测量hrv
   */
  stopMeasureHrv() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, stop, measureHrvData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },
    /**
   * 开始测量血压
   */
  startMeasureBloodPressure() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, start, measureBpData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
        this.queryRealTimeHealthData()
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   *手动停止测量血压
   */
  stopMeasureBloodPressure() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, stop, measureBpData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },

      /**
   * 开始测量压力
   */
  startMeasurePressure() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, start, measurePressData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
        this.queryRealTimeHealthData()
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   *手动停止测量压力
   */
  stopMeasurePressure() {
    appControlAPI.measureHealthData(queryHealthDataInvasiveMeasurement, stop, measurePressData, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },






  /**
   * 获取设备所支持的功能数
   */
  queryDevcieSupport(){
    appControlAPI.queryDevcieSupportFunction((state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
        })
      }
      alertUtil.closeToast()
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    // 隐藏页面(进入后台)关闭实时获取数据
    this.stopQueryHeartRate()
    this.stopQueryStep()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 退出页面关闭实时获取数据
    this.stopQueryHeartRate()
    this.stopQueryStep()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})